package com.situ.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.situ.dao.DanjuDao;
import com.situ.entily.Commodity;
import com.situ.entily.Danju;
import com.situ.entily.User;
import com.situ.service.CommodityService;
import com.situ.service.DanjuService;
import com.situ.vo.DanjuVO;
import com.situ.vo.ResultVO;

public class DanjuServiceImpl implements DanjuService {
	DanjuDao dao=new DanjuDao();
	
	@Override
	public List<DanjuVO> all(Integer page, Integer limit, String keyword,Date keyword2) {
		return dao.all((page - 1) * limit, limit, keyword,keyword2);
	}

	@Override
	public Long count(String keyword,Date keyword2) {
		return dao.count(keyword,keyword2);
	}

	@Override
	public ResultVO save(Danju d , HttpSession session) {
		ResultVO vo=new ResultVO();
		if(d.getDjId()==null) {
			CommodityService cs=new CommodityServiceImpl();
			Commodity comm =cs.selectById(d.getCommId());

			if (d.getDjCrk().equals("出库")) {
				if(d.getDjQuantity().compareTo(comm.getCommQuantity())==1){
					vo.setCode(1);
					vo.setMsg("仓库数量不足");
				}else {
					vo.setMsg("出库成功");
					BigDecimal bd=comm.getCommQuantity().subtract(d.getDjQuantity());
					d.setDjTime(LocalDateTime.now());
					//通过session获取是谁登录的系统
					Object obj=session.getAttribute("currentUser");
					User user=(User) obj;
					d.setDjCreateby(user.getUserName());
					dao.insert(d);
					dao.insert1(d,bd);
				}
			}else if(d.getDjCrk().equals("入库")){
				vo.setMsg("入库成功");
				BigDecimal bd=comm.getCommQuantity().add(d.getDjQuantity());
				d.setDjTime(LocalDateTime.now());
				Object obj=session.getAttribute("currentUser");
				User user=(User) obj;
				d.setDjCreateby(user.getUserName());
				dao.insert(d);
				dao.insert1(d,bd);
			}

			
		}else {
			vo.setMsg("修改成功");
			dao.update(d);
		}
		return vo;
	}

	@Override
	public void delete(Integer id) {
		dao.delete(id);	
	}

	@Override
	public DanjuVO selectById(Integer id) {
		return dao.selectById(id);
	}




	
	
}
